package csc.fresher.view;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import csc.fresher.entity.SystemRecordLogin;
import csc.fresher.service.SystemUserService;

@Component
@Scope("session")
public class SystemRecordLoginManageBean implements Serializable {
	private static final long serialVersionUID = 1L;
	private List<SystemRecordLogin> listRecord;
	private String searchValue;
	private DataModel<SystemRecordLogin> systemRecordListDataModel;
	private SystemRecordLogin Record;
	private Date toDate = new Date();
	private Date fromDate= new Date(toDate.getYear(),toDate.getMonth(),toDate.getDate()-30);


	@Autowired
	private SystemUserService systemUserService;

	@PostConstruct
	public void init() {
		this.Record = null;
		this.listRecord = firstLoad();
		this.systemRecordListDataModel = new ListDataModel<SystemRecordLogin>(this.listRecord);
	}

	// first load
	public List<SystemRecordLogin> firstLoad() {
		listRecord = systemUserService.getRecordLogin();
		return listRecord;
	}

	// search button
	public String search() {
		Date timeX= toDate;
		if (!"".equals(searchValue)){
			systemRecordListDataModel = new ListDataModel<SystemRecordLogin>(systemUserService.getRecordLoginByID((Long.parseLong(searchValue)),new Timestamp(fromDate.getTime()), new Timestamp(toDate.getTime()+86400000)));
		}else{
			systemRecordListDataModel = new ListDataModel<SystemRecordLogin>(systemUserService.getRecordLoginByDate(new Timestamp(fromDate.getTime()), new Timestamp(toDate.getTime()+86400000)));
		}return "RecordLogin";
	}

	public List<SystemRecordLogin> getListRecord() {
		return listRecord;
	}

	public void setListRecord(List<SystemRecordLogin> listRecord) {
		this.listRecord = listRecord;
	}

	public String getSearchValue() {
		return searchValue;
	}

	public void setSearchValue(String searchValue) {
		this.searchValue = searchValue;
	}

	public DataModel<SystemRecordLogin> getSystemRecordListDataModel() {
		return systemRecordListDataModel;
	}

	public void setSystemRecordListDataModel(
			DataModel<SystemRecordLogin> systemRecordListDataModel) {
		this.systemRecordListDataModel = systemRecordListDataModel;
	}

	public SystemRecordLogin getRecord() {
		return Record;
	}

	public void setRecord(SystemRecordLogin record) {
		Record = record;
	}

	public Date getFromDate() {
		return fromDate;
	}

	public void setFromDate(Date fromDate) {
		this.fromDate = fromDate;
	}

	public Date getToDate() {
		return toDate;
	}

	public void setToDate(Date toDate) {
		this.toDate = toDate;
	}

}
